import * as echarts from '../../ec-canvas/echarts';

const app = getApp();
var dataList = [];
var k = 0;
var Chart = null;
var day=[];

Page({

    /**
     * 页面的初始数据
     */
    data: {
        ec: {   }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;
      
        var parent_id=options.parent_id;
        if(parent_id!=undefined){
            this.setData({
                parent_id:parent_id
              })
              wx.setStorageSync('parent_id',parent_id);
        }else{
            parent_id = wx.getStorageSync('parent_id');
        
        }
        var userInfo=wx.getStorageSync('userInfo');
        if(userInfo==""){
            wx.navigateTo({
              url: '../login/index?parent_id='+parent_id,
            })
        }
        wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })      
        that.setData({
            platform:app.globalData.platform,    
            userInfo:userInfo,
             width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.domain
          })
    },
  
    
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        console.log(this.data.ec);
       // this.getIndex();
        this.echartsComponnet = this.selectComponent('#mychart-dom-line');
        this.getData(); //获取数据
    },
    getData: function () {
      let that = this; 
      var userInfo=wx.getStorageSync('userInfo'); 
        /**
         * 此处的操作：
         * 获取数据json
         */
        /*
      if(k % 2){
        dataList = [1, 2, 3, 4, 5, 6,9];
      }else{
        dataList = [7, 6, 9, 2, 5, 6,1];
      }
      day=['88.10', '18.21', '18.22', '18.23', '18.24', '18.25', '18.26'];
      k++;
      //如果是第一次绘制
      if (!Chart){
        this.init_echarts(); //初始化图表
      }else{
        this.setOption(Chart); //更新数据
      }
      */
     wx.request({
      url: app.globalData.domain+'rank/getMonth',
      data:{from:1,user_id:userInfo.id},
       // data: data,
       // method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: (res) => {
          console.log(res)
          if(res.statusCode=200){
            var data=res.data;
            dataList = data.list;
            day=data.day;
           var  month=data.month;
            that.setData({
              month:month
            })
           // if (!Chart){
              this.init_echarts(); //初始化图表
            /*}else{
              this.setOption(Chart); //更新数据
            }
            */
          }
          
          //this.init_echarts();//初始化图表
        }
      });  
    },
    //初始化图表
    init_echarts: function () {
        this.echartsComponnet.init((canvas, width, height) => {
          // 初始化图表
          Chart = echarts.init(canvas, null, {
            width: width,
            height: height
          });
          // Chart.setOption(this.getOption());
          this.setOption(Chart);
          // 注意这里一定要返回 chart 实例，否则会影响事件处理等
          return Chart;
        });
      },
      setOption: function (Chart) {
        // Chart.clear();  // 清除
        Chart.setOption(this.getOption());  //获取新数据
      },
      getOption: function () {
        // 指定图表的配置项和数据
        var option = {
          tooltip: {
            show: true,
            trigger: 'axis'
          },
          xAxis: {
            type: 'category',
            data: day
          },
          yAxis: {
            type: 'value'
          },
          series: [{
            data: dataList,
            type: 'line'
          }]
        }
        return option;
      },
      goBack(){
        wx.navigateBack();
      },
    getIndex:function(){
        let that = this; 
        var userInfo=wx.getStorageSync('userInfo');       
        wx.request({
          url: app.globalData.domain+'rank/getIndex',
          data:{from:1,user_id:userInfo.id},
          success:res=>{             
              var data=res.data;
              console.log(data);
             // var my=data.my
              //var mylen=my.length; 
            that.setData({
             shareTitle:data.shareTitle,
             shareLogo:data.shareLogo,
              list:data.list,
              my:data.my,
              mylen:data.mylen,
              count:data.count
            })
          }
      })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareTimeline(){
        //console.log(e);
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareLogo;
        var userInfo=wx.getStorageSync('userInfo');

        var parent_id=userInfo.id;
         this.setData({
           share:true
         });
         console.log(shareLogo);
         console.log(title);
         return{
           title: title,                 
           query: 'parent_id='+parent_id,        
           imageUrl:shareLogo      
           }
       },
       onShareAppMessage(e) {
        var userInfo=wx.getStorageSync('userInfo');
        var parent_id=userInfo.id;
        var that = this;
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareLogo;
   　　var shareObj = {
   　　　　title: title,    
   　　　　path: '/pages/index/index?parent_id='+parent_id,   
   　　　　imageUrl: shareLogo 
   　　　
   　　}
   　　// 来自页面内的按钮的转发
   　　if( e.from == 'button' ){
     　　　shareObj.title=title;
   　　　　shareObj.path = '/pages/index/index?     parent_id='+parent_id;
   　　}
   console.log(shareObj);
   　　// 返回shareObj
   　　return shareObj;
    }
})